package com.shengyuan.manage_os.old.ying.erpm.tasks;

import com.shengyuan.manage_os.old.ying.erpm.Actual_OA_DaKa_JiDong;
import com.shengyuan.manage_os.old.ying.erpm.LoginActivity;
import com.shengyuan.manage_os.old.ying.erpm.MainApplication;
import com.shengyuan.manage_os.old.ying.erpm.controls.CurrentPersonal;
import com.shengyuan.manage_os.old.ying.erpm.controls.CurrentTime;
import com.shengyuan.manage_os.old.ying.erpm.db.Actual_Data_Table;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

public class AlarmReceiver extends BroadcastReceiver{

	private Context context;

	@Override
	public void onReceive(Context context, Intent intent) {
		this.context = context;
		if (intent.getAction().equals("daka_repeating")) {
			String sdate = CurrentTime.getShortTime();
			String[] source = sdate.split(":");
			String[] setting =  LoginActivity.mSettingArr;
			if (setting!=null) {
				for (int i = 0; i < setting.length; i++) {
					String[] dest = setting[i].split(":");
					if (dest[0].equals(source[0]) && dest[1].equals(source[1]) ) {
						Message msg = new Message();
						msg.what = 110;
						msg.obj = source[0] + ":" + source[1] ;
						mHandler_DaKa.sendMessage(msg);
					}
				}
			}
		}
	}

	public Handler mHandler_DaKa= new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case 110:
					String oString = msg.obj.toString();
					if (Actual_OA_DaKa_JiDong.HaveCreate || Actual_OA_DaKa_JiDong.nowTime.equals(oString)) {
						return;
					}
					Actual_Data_Table dbo = new Actual_Data_Table(MainApplication.Instance);
					int k  = dbo.get_Sended_DaKa();
					dbo.close();
					if ( k==2 || k==0 ) {
						Utility.MessageBox(context,"已经下班了");
						return;
					}else {
						Utility.MessageBox(context,"已经在上班中，你要补打记录！");
					}
					Actual_OA_DaKa_JiDong.HaveCreate = true;
					Actual_OA_DaKa_JiDong.nowTime = oString;

					CurrentPersonal.UserName = LoginActivity.pmUser.UserName;
					Intent intent2 = new Intent( MainApplication.Instance, Actual_OA_DaKa_JiDong.class);
					intent2.putExtra("DengJi", "定位");
					intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					MainApplication.Instance.startActivity(intent2);
					break;
			}
		}
	};

	public AlarmReceiver() {
	}

}
